
function showLoginForm(){
    $('#loginModal .registerBox').fadeOut('fast',function(){
        $('.loginBox').fadeIn('fast');
        $('.register-footer').fadeOut('fast',function(){
            $('.login-footer').fadeIn('fast');    
        });
        
        $('.modal-title').html('Login with');
    });       
     $('.error').removeClass('alert alert-danger').html(''); 
}

function openLoginModal(){
    showLoginForm();
    setTimeout(function(){
        $('#loginModal').modal('show');    
    }, 230);
    
}

function loginAjax(url){
    var oAjax;
    if(window.XMLHttpRequest){
        oAjax = new XMLHttpRequest();
    }else{
        oAjax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    oAjax.onreadystatechange = function(){
        if(oAjax.readyState==4){
            var contexts = oAjax.responseText;
            if(oAjax.status==200){
            	if(url=="login!doLoginAction"){
            		if(contexts=="密码错误"){
                		$('.error').addClass('alert alert-danger').html("密码错误！");
                		$('#loginModal .modal-dialog').addClass('shake');
                        shake();
                	}else{
                		$(function () { $('#loginModal').modal('hide')});
                		document.getElementById("doLogin").innerHTML = contexts;
                	}
            	}else{
            		if(contexts!="账号存在"){
                		$('.error').addClass('alert alert-danger').html("账号不存在！");
                		$('#loginModal .modal-dialog').addClass('shake');
                        shake();
                	}else{
                		$('.error').addClass('alert alert-danger').html("");
                	    $('.error').removeClass('alert alert-danger');
                	}
            	}
            }
            else if(oAjax.status==404)
                alert("错误404：未找到资源！");
            else if(oAjax.status==500)
                alert("错误500：服务器繁忙！");
        }else{

        }
    }
    
    oAjax.open("post",url);
    oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    //获取form表单对象
    var oForms = document.forms["loginform"];
    //send()方法所需传递参数
    var Parameters = "mst_Account="+oForms.mst_Account.value+"&mst_Password="+oForms.mst_Password.value;
    oAjax.send(Parameters);
}

function shakeModal(){
    $('#loginModal .modal-dialog').addClass('shake');
    $('input[type="password"]').val('');
    shake();
}

function shake(){
    setTimeout( function(){
        $('#loginModal .modal-dialog').removeClass('shake');
    }, 1000 );
}

function validatedAccount(oInput){
    var reg = /[^0-9]/;
    var value = oInput.value;
    if(reg.test(value)||value==""||value.length!=11){
        if(value==""){
            $('.error').addClass('alert alert-danger').html("账号不能为空！");
        }
        if(value.length!=11){
            $('.error').addClass('alert alert-danger').html("账号输入长度应为11位<small>(手机号码)</small>");
        }else{
            $('.error').addClass('alert alert-danger').html("账号输入类型应为数字<small>(手机号码)</small>");
        }
        oInput.style.background = "#cccccc";
        $('#loginModal .modal-dialog').addClass('shake');
        shake();
    }else{
    	loginAjax("login!doValidatedAction");
        $('.error').addClass('alert alert-danger').html("");
        $('.error').removeClass('alert alert-danger');
        oInput.style.background = "#78ff64";
    }
}
function validatedPasswd(oPasswd){
    var value = oPasswd.value;
    var reg = /[^0-9a-zA-Z]/;
    if(reg.test(value)||value==""||value.length<6){
        if(value==""){
            $('.error').addClass('alert alert-danger').html("密码不能为空！");
        }
        if(value.length<6){
            $('.error').addClass('alert alert-danger').html("密码长度不能小于6位！");
        }else{
            $('.error').addClass('alert alert-danger').html("密码只能由数字和大小写字母组成！");
        }
        oPasswd.style.background = "#cccccc";
        $('#loginModal .modal-dialog').addClass('shake');
        shake();
    }else{

        $('.error').addClass('alert alert-danger').html("");
        $('.error').removeClass('alert alert-danger');
        oPasswd.style.background = "#78ff64";
    }
}
   